//
//  ReadingRoomViewController.m
//  厚积薄发
//
//  Created by 侯振兴 on 14/12/31.
//  Copyright (c) 2014年 侯振兴. All rights reserved.
//

#import "ReadingRoomViewController.h"
#import "ClassificationController.h"
#import "ConnectModel.h"
#import "ReadingRoomModel.h"
#import "NewsCollectionViewCell.h"
#import "ConnectUrl.h"
#import "HideCollectionViewCell.h"
#import "HighCollectionViewCell.h"
#import "DetailsViewController.h"
#import "ArtCollectionViewCell.h"


@interface ReadingRoomViewController ()

@end

@implementation ReadingRoomViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"SHARE" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"CLASSTONEWS" object:nil];
   

    [_dics release];
    [_have release];
    [_allArray release];
    [_array release];
    [_collection release];
    [_user release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray arrayWithObjects:@"精品", @"转藏", @"社会", @"政治", @"历史", @"文化", @"财经", @"科学", @"健康", @"生活", @"美容", @"美食", @"家具", @"育儿", @"星座", @"休闲", @"情感", @"心灵", @"幽默", @"美文", @"教育", @"职场", @"电脑", @"爱车", @"旅游", @"摄影", @"艺术", @"美图", @"原创", nil];
        self.allArray = [NSMutableArray array];
        NSArray *array1 = [NSArray arrayWithObjects:JingPin, @"-2", nil];
        NSArray *array2 = [NSArray arrayWithObjects:Hide, @"-1", nil];
        NSArray *array3 = [NSArray arrayWithObjects:Social, @"6", nil];
        NSArray *array4 = [NSArray arrayWithObjects:Political, @"9", nil];
        NSArray *array5 = [NSArray arrayWithObjects:History, @"7", nil];
        NSArray *array6 = [NSArray arrayWithObjects:Culture, @"8", nil];
        NSArray *array7 = [NSArray arrayWithObjects:Economics, @"10", nil];
        NSArray *array8 = [NSArray arrayWithObjects:Science, @"12", nil];
        NSArray *array9 = [NSArray arrayWithObjects:Healthy, @"16", nil];
        NSArray *array10 = [NSArray arrayWithObjects:Life, @"1", nil];
        NSArray *array11 = [NSArray arrayWithObjects:Beauty, @"2", nil];
        NSArray *array12 = [NSArray arrayWithObjects:Food, @"17", nil];
        NSArray *array13 = [NSArray arrayWithObjects:Furniturn, @"4", nil];
        NSArray *array14 = [NSArray arrayWithObjects:Parenting, @"5", nil];
        NSArray *array15 = [NSArray arrayWithObjects:Constellation, @"3", nil];
        NSArray *array16 = [NSArray arrayWithObjects:Leisure, @"27", nil];
        NSArray *array17 = [NSArray arrayWithObjects:Emotional, @"14", nil];
        NSArray *array18 = [NSArray arrayWithObjects:Mind, @"13", nil];
        NSArray *array19 = [NSArray arrayWithObjects:Humor, @"18", nil];
        NSArray *array20 = [NSArray arrayWithObjects:Note, @"15", nil];
        NSArray *array21 = [NSArray arrayWithObjects:Eduction, @"26", nil];
        NSArray *array22 = [NSArray arrayWithObjects:Workplace, @"24", nil];
        NSArray *array23 = [NSArray arrayWithObjects:Computer, @"25", nil];
        NSArray *array24 = [NSArray arrayWithObjects:Car, @"23", nil];
        NSArray *array25 = [NSArray arrayWithObjects:Tourism, @"11", nil];
        NSArray *array26 = [NSArray arrayWithObjects:Photography, @"22", nil];
        NSArray *array27 = [NSArray arrayWithObjects:Art, @"21", nil];
        NSArray *array28 = [NSArray arrayWithObjects:Beautiful, @"20", nil];
        NSArray *array29 = [NSArray arrayWithObjects:Original, @"28", nil];
        
        self.dics =[NSMutableDictionary dictionaryWithObjectsAndKeys:array1, @"精品接口", array2, @"转藏接口", array3, @"社会接口", array4, @"政治接口", array5, @"历史接口", array6, @"文化接口", array7, @"财经接口", array8, @"科学接口", array9, @"健康接口", array10, @"生活接口", array11, @"美容接口", array12, @"美食接口", array13, @"家具接口", array14, @"育儿接口", array15, @"星座接口", array16, @"休闲接口", array17, @"情感接口", array18, @"心灵接口", array19, @"幽默接口", array20, @"美文接口", array21, @"教育接口", array22, @"职场接口", array23, @"电脑接口", array24, @"爱车接口", array25, @"旅游接口", array26, @"摄影接口", array27, @"艺术接口", array28, @"美图接口", array29, @"原创接口", nil];
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"阅览区";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"CLASSTONEWS" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toDetails:) name:@"SHARE" object:nil];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setFrame:CGRectMake(0, 0, 25, 25)];
    [button setBackgroundImage:[UIImage imageNamed:@"liebiao.png"] forState:UIControlStateNormal];
    button.alpha = 0.5;
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
   //    self.navigationController.navigationBarHidden = YES;
    _currStr = @"精品接口";
    [self createHeader];
    [self createCollection];

}
- (void)toDetails: (NSNotification *)noti
{
    ReadingRoomModel *model = [noti.userInfo objectForKey:@"key"];
    DetailsViewController *detail = [[DetailsViewController alloc] init];
    detail.parameter = model;
    [self presentViewController:detail animated:NO completion:nil];
    [detail release];
}

#warning class通知
- (void)changeColor: (NSNotification *)noti
{
    NSString *str = [noti.userInfo objectForKey:@"text"];
    UIScrollView * scroll = (UIScrollView *)[self.view viewWithTag:10];
    NSInteger value = 0;
    NSInteger offset = 0;
    for (int i = 0; i < [_array count]; i++)
    {
        if ([str isEqualToString:[_array objectAtIndex:i]]) {
            value = i;
            offset = i;
            if (i > 25) {
                value = 25;
            }
            scroll.contentOffset = CGPointMake(70 * value, 0);
            UIButton *button = (UIButton *)[scroll viewWithTag:100 + i];
            for (int i = 0; i < 29; i++) {
                UIButton *allButton = (UIButton *)[scroll viewWithTag:100 + i];
                [allButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                allButton.backgroundColor = [UIColor whiteColor];
            }
            
            NSInteger red = arc4random()%256;
            NSInteger blue = arc4random()%256;
            NSInteger green = arc4random()%256;
            
            UIColor *color = [[UIColor alloc] initWithRed:red/256.0f green:green/256.0f blue:blue/256.0f alpha:1] ;
            
            button.backgroundColor = color;
            [color release];
            [button setTitleColor:[UIColor whiteColor] forState: UIControlStateNormal];
            self.currStr = [NSString stringWithFormat:@"%@接口",[_array objectAtIndex:button.tag - 100]];
        }
    }
    _collection.contentOffset = CGPointMake(self.view.frame.size.width * offset, 0);
    [_collection reloadData];
}

- (void)buttonAction
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NEWSTOUSER" object:self];
}

#warning createCollection
- (void)createCollection
{
    UICollectionViewFlowLayout *flow = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flow.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 200);
    flow.minimumLineSpacing = 0;
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 200) collectionViewLayout:flow];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection];
    [_collection release];
    NSInteger value = 0;
    for (int i = 0 ; i < [_array count]; i++)
    {
        NSString * str = [NSString stringWithFormat:@"%@接口",[_array objectAtIndex:i]];
        if ([str isEqualToString:_currStr])
        {
            
            value = i;
            _collection.contentOffset = CGPointMake(self.view.frame.size.width * value, 0);
        }
    }
    
    _collection.contentSize = CGSizeMake(self.view.frame.size.width * [_array count], self.view.frame.size.height - 64);
    _collection.pagingEnabled = YES;
    for (int i = 0; i < [_array count]; i++) {
        NSString *str = [_array objectAtIndex:i];
        if ([str isEqualToString:@"精品"]) {
            [_collection registerClass:[HighCollectionViewCell class] forCellWithReuseIdentifier:@"highCollectioncell"];
        }else if ([str isEqualToString:@"转藏"]){
            [_collection registerClass:[HideCollectionViewCell class] forCellWithReuseIdentifier:@"hideCollectioncell"];
        }else if ([str isEqualToString:@"艺术"] || [str isEqualToString:@"美图"]){
            [_collection registerClass:[ArtCollectionViewCell class] forCellWithReuseIdentifier:str];
        }else{
            [_collection registerClass:[NewsCollectionViewCell class] forCellWithReuseIdentifier:str];
        }
    }
    
    [_collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    UIScrollView * scroll = (UIScrollView *)[self.view viewWithTag:10];
    NSInteger value = 0;
    for (int i = 0; i < [_array count]; i++)
    {
        if ([[_array objectAtIndex: (int)_collection.contentOffset.x / self.view.frame.size.width] isEqualToString:[_array objectAtIndex:i]])
        {
            value = i;
            if (i > 25){
                value = 25;
            }
            
            scroll.contentOffset = CGPointMake(70 * value, 0);
            UIButton *button = (UIButton *)[scroll viewWithTag:i + 100];
            for (int i = 0; i < 29; i++)
            {
                UIButton *allButton = (UIButton *)[scroll viewWithTag:100 + i];
                [allButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                allButton.backgroundColor = [UIColor whiteColor];
            }
            NSInteger red = arc4random()%256;
            NSInteger blue = arc4random()%256;
            NSInteger green = arc4random()%256;
            UIColor *color = [[UIColor alloc] initWithRed:red/256.0f green:green/256.0f blue:blue/256.0f alpha:1] ;
            button.backgroundColor = color;
            [color release];
            [button setTitleColor:[UIColor whiteColor] forState: UIControlStateNormal];
            self.currStr = [NSString stringWithFormat:@"%@接口",[_array objectAtIndex:button.tag - 100]];
            break;
        }
    }
    [_collection reloadData];
}
#warning viewHeader
- (void)createHeader
{
    UIScrollView * scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 10, self.view.frame.size.width - 40, 30)];
    scroll.tag = 10;
    scroll.layer.cornerRadius = 9;
    scroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:scroll];
    [scroll release];
    scroll.contentSize = CGSizeMake(29 * 70, 30);
    scroll.bounces = NO;
    for (int i = 0; i < 29; i++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(70 * i, 0, 60, 30)];
        button.tag = 100 + i;
        button.layer.cornerRadius = 10;
        NSString *str = [_array objectAtIndex:i];
        [button setTitle:str forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(goTo:) forControlEvents:UIControlEventTouchUpInside];
        [scroll addSubview:button];
        [button release];
    }
    UIButton *button = (UIButton *)[scroll viewWithTag:100];
    NSInteger red = arc4random()%256;
    NSInteger blue = arc4random()%256;
    NSInteger green = arc4random()%256;
    UIColor *color = [[UIColor alloc] initWithRed:red/256.0f green:green/256.0f blue:blue/256.0f alpha:1];
    button.backgroundColor = color;
    [color release];
    [button setTitleColor:[UIColor whiteColor] forState: UIControlStateNormal];
}

- (void)goTo: (UIButton *)button
{
    UIScrollView *scroll = (UIScrollView *)[self.view viewWithTag:10];
    for (int i = 0; i < 29; i++) {
        UIButton *allButton = (UIButton *)[scroll viewWithTag:100 + i];
        [allButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        allButton.backgroundColor = [UIColor whiteColor];
    }
    _collection.contentOffset = CGPointMake(self.view.frame.size.width * (button.tag - 100), 0);
    
    NSInteger red = arc4random()%256;
    NSInteger blue = arc4random()%256;
    NSInteger green = arc4random()%256;
    UIColor *color = [[UIColor alloc] initWithRed:red/256.0f green:green/256.0f blue:blue/256.0f alpha:1];
    button.backgroundColor = color;
    [color release];
    [button setTitleColor:[UIColor whiteColor] forState: UIControlStateNormal];
    self.currStr = [NSString stringWithFormat:@"%@接口",[_array objectAtIndex:button.tag - 100]];
    [_collection reloadData];
}

#warning collectionCell

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    [_allArray removeAllObjects];
    [_allArray addObjectsFromArray:[_dics objectForKey:_currStr]];
    NSLog(@"%@",_currStr);
    for (int i = 0 ; i < [_array count]; i++)
    {
        NSString * str = [NSString stringWithFormat:@"%@接口",[_array objectAtIndex:i]];
        if ([str isEqualToString:_currStr])
        {
            _value = i;
            break;
        }
    }
    return [_array count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([_currStr isEqualToString:@"精品接口"]) {
        HighCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"highCollectioncell" forIndexPath:indexPath];
        [cell.array removeAllObjects];
        cell.curStr = [NSString stringWithFormat:@"%@",_currStr];
        [cell.array addObjectsFromArray:_allArray];
        return cell;
    }
    else if ([_currStr isEqualToString:@"转藏接口"]) {
        HideCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"hideCollectioncell" forIndexPath:indexPath];
        [cell.array removeAllObjects];
        cell.curStr = [NSString stringWithFormat:@"%@",_currStr];
        [cell.array addObjectsFromArray:_allArray];
        return cell;
    }
    else if ([_currStr isEqualToString:@"艺术接口"] || [_currStr isEqualToString:@"美图接口"]){
        NSString *str = [_currStr stringByReplacingOccurrencesOfString:@"接口" withString:@""];
        ArtCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
        [cell.array removeAllObjects];
        cell.curStr = [NSString stringWithFormat:@"%@",_currStr];
        [cell.array addObjectsFromArray:_allArray];
        return cell;
    }
    else
    {
        NSString *str = [_currStr stringByReplacingOccurrencesOfString:@"接口" withString:@""];
        NewsCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
        [cell.array removeAllObjects];
        cell.curStr = [NSString stringWithFormat:@"%@",_currStr];
        [cell.array addObjectsFromArray:_allArray];
        return cell;
    }
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    return cell;
    //    if ([self.reads containsObject:@"http1"]) {
    //        [cell getDataFromLocal:@"http1"];
    //    } else {
    //        [cell connect:@"http1:"];
    //        [self.reads addObject:@"http1"];
    //    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
